<?php

class Dispatcher {
	
	protected static $_instance;
	protected $_defaultModule;
	protected $_defaultController;
	protected $_defaultAction;
	protected $_request;
	protected $_view;
	protected $_moduleName;
	protected $_controllerName;
	protected $_actionName;
	
	private function __construct() {}
	private function __clone() {}
	
	/**
	 * 获取对象单例
	 *
	 * @return object
	 */
	public static function getInstance() {
		if (!(self::$_instance instanceof self)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	/**
	 * 设置默认模块名
	 *
	 * @param string $name
	 */
	public function setDefaultModule($name) {
		$this->_defaultModule = $name;
	}
	
	/**
	 * 获取默认模块名
	 *
	 * @return string
	 */
	public function getDefaultModule() {
		return $this->_defaultModule;
	}
	
	/**
	 * 设置默认控制器名
	 *
	 * @param string $name
	 */
	public function setDefaultController($name) {
		$this->_defaultController = $name;
	}
	
	/**
	 * 获取默认控制器名
	 *
	 * @return string
	 */
	public function getDefaultController() {
		return $this->_defaultController;
	}
	
	/**
	 * 设置默认动作名
	 *
	 * @param string $name
	 */
	public function setDefaultAction($name) {
		$this->_defaultAction = $name;
	}
	
	/**
	 * 获取默认动作名
	 *
	 * @return string
	 */
	public function getDefaultAction() {
		return $this->_defaultAction;
	}
	
	/**
	 * 设置请求对象
	 *
	 * @param Request $rq
	 */
	public function setRequest(Request $rq) {
		$this->_request = $rq;
	}
	
	/**
	 * 获取请求对象
	 *
	 * @return object
	 */
	public function getRequest() {
		return $this->_request;
	}
	
	/**
	 * 设置视图对象
	 *
	 * @param View_Interface $view
	 */
	public function setView(View_Interface $view) {
		$this->_view = $view;
	}
	
	/**
	 * 获取视图对象
	 *
	 * @return object
	 */
	public function getView() {
		return $this->_view;
	}
	
	/**
	 * 获取请求模块名
	 *
	 * @return string
	 */
	public function getModuleName() {
		if (empty($this->_moduleName)) {
			return $this->getDefaultModule();
		}
		return $this->_moduleName;
	}
	
	/**
	 * 获取请求控制器名
	 *
	 * @return string
	 */
	public function getControllerName() {
		if (empty($this->_controllerName)) {
			return $this->getDefaultController();
		}
		return $this->_controllerName;
	}
	
	/**
	 * 获取请求动作名
	 *
	 * @return string
	 */
	public function getActionName() {
		if (empty($this->_actionName)) {
			return $this->getDefaultAction();
		}
		return $this->_actionName;
	}
	
	public function dispatch() {
		$queryString = $this->_request->getServer('QUERY_STRING');
		$this->_moduleName = $this->_request->getQuery('m');
		$this->_controllerName = $this->_request->getQuery('c');
		$this->_actionName = $this->_request->getQuery('a');
		
		$modName = $this->getModuleName();
		$conName = $this->getControllerName();
		$actName = $this->getActionName();
		
		$className = "{$conName}Controller";
		if (class_exists($className)) {
			$controller = new $className();
			if (method_exists($controller, "{$actName}Action")) {
				$ret = call_user_func(array($controller, "{$actName}Action"),  $this->_request->getParams());
				if ($ret !== false) {
					//渲染模版
					
				}
				$controller->getResponse()->response(); //输出结果
			}
		}
	}
}